<?php
class User_Controller extends CI_controller
{
	function user_controller()
	{
		parent::__construct();
		$this->load->model('userModel', '', TRUE);
		$this->load->helper(array('form', 'url'));
		$this->load->helper('url');
	}
	
	function index()
 	{
	//   $this->load->view('home_template');
   		if ($this->session->userdata('username') !=NULL){
   		$this->load->view('userPage');
   		}else{
   		$this->load->view('loginForm');
   		}
 	}
	
	function userRegistrasi()
    {	
		$this->form_validation->set_rules('username','Username','required|max_length[20]|alpha_numeric');
		$this->form_validation->set_rules('password','Password','required|max_length[20]|min_lenght[5]|alpha_numeric');
		$this->form_validation->set_rules('jeniskelamin','Jenis Kelamin','required');
		$this->form_validation->set_rules('email','Email','required|max_length[100]|valid_email|unique[login.email]');
		$this->form_validation->set_rules('tanggallahir','Tanggal Lahir','required','date'); 		
 
        if($this->form_validation->run() == false)
        {         
           $confirmpass = $this->input->post('confirmpass');
		   //handle gender M/F
			$status = null;
			if (isset($_POST['save'])) {
			$status = $_POST['gender'];
			}
		   $jnsKelamin = $status;
		   $tanggallahir = $this->input->post('theDate');
		   $userData = array (
           'username' => $this->input->post('username'),
		   'password' => $this->input->post('password'),
		   'email' => $this->input->post('email'),
		   'birthdate' => $this->input->post('theDate'),
		   'gender' => $jnsKelamin,
		   
           );
          $user = null;
          $user = $this->User_Model->add_user($userData,$confirmpass);
		
		  if($user['status']== TRUE){
				$this->load->view('loginForm');
				}else{
				$this->load->view('pendaftaranForm');
				}
			}
        else{
            $this->load->view('pendaftaranForm');
			}	
    }
    
	function tambah_user()
	{
		if ($this->input->post('submit')) {
			$this->load->model('userModel');
			$this->userModel->add_users();
			redirect('login/index');
		}
		$this->load->view('pendaftaranForm');
	}
}
?>